const sequelize = require("./sequelize");
const userModel = require("./user");
const opts = {
  schema: {
    response: {
      200: {
        type: "object",
        properties: {
          code: { type: "number" },
          data: { token: "string" },
        },
      },
    },
    body: {
      type: "object",
      properties: {
        username: { type: "string" },
        password: { type: "string" },
      },
    },
  },
};
async function routes(fastify, options) {
  const User = await userModel(sequelize);

  fastify.post("/user/insert", opts, async (request, reply) => {
    const jane = await User.create({ firstName: "Jane", lastName: "Doe" });
    // java.firstName = 'test1'
    // await jane.save()
    // console.log("Jane's auto-generated ID:", jane.id);
    reply.send({ code: 20000, data: { token: "admin-token" } });
  });
  fastify.get("/user/update", async (request, reply) => {
    await User.update(
      { lastName: "Doe1" },
      {
        where: {
          id: 2,
        },
      }
    );
    // console.log("Jane's auto-generated ID:", jane.id);
    return {
      code: 0,
      msg: "更新成功",
    };
  });
  fastify.get("/user/select", async (request, reply) => {
    const users = await User.findAll();
    // console.log("Jane's auto-generated ID:", jane.id);
    return {
      code: 0,
      msg: "查询成功",
      data_json: users,
    };
  });
}

module.exports = routes;
